package com.xmu.edu.tisp.service.basedata;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xmu.edu.tisp.dao.Service;
import com.xmu.edu.tisp.dao.TISP_dbHelper;

public class XJSZXX_service implements Service{
	

	public static final String TABLE_NAME = "XJSZXX";
	public static final String PRIMARY_KEY="HTH";
	
	private SQLiteDatabase myDB;
	private SQLiteOpenHelper dbHelper;


	public XJSZXX_service(Context cxt) {
		dbHelper=new TISP_dbHelper(cxt, "TISP");
	}

	public SQLiteDatabase getdb()
	{
		if(myDB==null)
		{
			myDB=dbHelper.getWritableDatabase();
			return myDB;
		}
		return myDB;
	}
	
	@Override
	public void g_updata(ContentValues values) {
		getdb().update(TABLE_NAME, values, PRIMARY_KEY+"=?", new String[]{(String) values.get(PRIMARY_KEY)});
		System.out.println(TABLE_NAME+"have updataed!");
		
	}

	@Override
	public void g_insert(ContentValues values) {
		getdb().insert(TABLE_NAME, null, values);
		
		System.out.println(TABLE_NAME+"have inserted!");
	}

	@Override
	public void g_delete(String[] id) {
		getdb().delete(TABLE_NAME, PRIMARY_KEY+"=?", id);
		System.out.println(TABLE_NAME+"have deleted!");
		
	}


	@Override
	public List<String> g_getIDs() {
		Cursor c=getdb().query(TABLE_NAME, new String[]{PRIMARY_KEY},null, null, null, null, null);
		List<String> result=new ArrayList<String>();
		while(c.moveToNext())
		{
			String id=c.getString(c.getColumnIndex(PRIMARY_KEY));
			result.add(id);
		}
		return result;
	}
	public void close()
	{
		getdb().close();
		//System.out.println("db closed");
	}
	
	/*
	 * by feng
	 */
	
	/*
	   *   lxdm   <------- farmer_cd  (hth)    
	   *   vector  <-------- String  
	   *   test   ok
	   */
	  public Vector getLxdm_from_farmerCD(String farmerCD)
	  {
		  	Vector lxdm=new Vector<String>();
			String sql="";
			sql="select  LXDM from XJSZXX where HTH is '"+farmerCD+"'" ;  
			Cursor cursor = getdb().rawQuery(sql, null);
			while(cursor.moveToNext()) {
				lxdm.add(cursor.getString(cursor.getColumnIndex("LXDM")));	
			}		
			cursor.close();
			return lxdm;
	  }

	
	
	
	
	
	
}
